package org.example.domain.system.model;


import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.FieldFill;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import org.example.utils.DateUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.text.ParseException;
import java.util.Date;

/**
 * 领域对象（Domain Model）
 * 用户表 （也可用做企业内部服务表， 服务即用户）
 */
@TableName("user_account")
@Schema(description = "用户表")
public class UserAccount {

    @Schema(description = "用户Id")
    private Long userId;
    @Schema(description = "用户名")
    private String userName;
    @Schema(description = "密码")
    private String password;
    @Schema(description = "邮箱")
    private String email;
    @Schema(description = "手机号")
    private String phoneNumber;
    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "UTF-8")
    @DateTimeFormat(pattern = "yyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "UTF-8")
    @DateTimeFormat(pattern = "yyy-MM-dd HH:mm:ss")
    @Schema(description = "更新时间")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "UTF-8")
    @DateTimeFormat(pattern = "yyy-MM-dd HH:mm:ss")
    @Schema(description = "删除时间")
    private Date deleteTime;
    @Schema(description = "删除标识, true:已删除, false:未删除")
    @TableField(fill = FieldFill.INSERT)
    private Boolean deleteIdentity;
    @Schema(description = "是否停用, true:已停用, false:未停用")
    @TableField(fill = FieldFill.INSERT)
    private Boolean status;
    @Schema(description = "过期时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "UTF-8")
    @DateTimeFormat(pattern = "yyy-MM-dd HH:mm:ss")
    private Date expirationDate;

    public Date getExpirationDate() {
        try {
            return DateUtils.parseDate("9999-12-31 23:59:59", DateUtils.YYYY_MM_DD_HH_MM_SS);
        } catch (ParseException e) {
            return new Date();
        }

    }


    /**
     * 生成BCryptPasswordEncoder密码
     *
     * @param password 真实密码
     */
    public void encryptPassword(String password) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        this.password = passwordEncoder.encode(password);
    }

    /**
     * 判断密码是否相同
     *
     * @param rawPassword 加密前密码
     * @return 结果
     */
    public boolean matchesPassword(String rawPassword) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.matches(rawPassword, this.password);
    }


    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public Date getDeleteTime() {
        return deleteTime;
    }

    public void setDeleteTime(Date deleteTime) {
        this.deleteTime = deleteTime;
    }

    public Boolean getDeleteIdentity() {
        return deleteIdentity;
    }

    public void setDeleteIdentity(Boolean deleteIdentity) {
        this.deleteIdentity = deleteIdentity;
    }

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    public void setExpirationDate(Date expirationDate) {
        this.expirationDate = expirationDate;
    }
}
